﻿using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace SimulationPTSystem.EnvironmentDevice.Input
{
    /// <summary>
    /// 配置数据读取
    /// </summary>
    public class ConfigDataConverter : IDataConverter
    {
        /// <summary>
        /// fhm
        /// </summary>
        #region "events"
        public event EventHandler<DataReceivedEventArgs> DataReceived;
        #endregion
        #region "private members"
        private bool isRunning = false;
        private string dataFileName = "configData.txt";           //配置数据文件名称
        private Hashtable ht = new Hashtable();                    //配置值列表
        private Stopwatch stopwatch = new Stopwatch();
        #endregion
        #region "Constructor"
        /// <summary>
        /// 默认40mm和configData.txt
        /// </summary>
        public ConfigDataConverter(string dataFileName = "")
        {
            if (!string.IsNullOrEmpty(dataFileName))
                this.dataFileName = dataFileName;
        }
        #endregion
        #region "public methods"
        public bool Initialize()
        {
            int index = 0;
            try
            {
                string[] lines = File.ReadAllLines(this.dataFileName, System.Text.Encoding.Default);         //加载配置数据文件
                for(int i=0;i<lines.Length;i++)
                {
                    index = i;
                    string[] strs = lines[i].Split(';');
                    string[] timeStrs = strs[0].Split(':');
                    ht.Add(new TimeSpan(0, Convert.ToInt32(timeStrs[0]), Convert.ToInt32(timeStrs[1]), Convert.ToInt32(timeStrs[2]), Convert.ToInt32(timeStrs[3]) * 40), new byte[] { Convert.ToByte(strs[1]), Convert.ToByte(strs[2]), Convert.ToByte(strs[3]) });
                }
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("加载配置数据文件失败:" + ex.Message);
            }
        }

        public Task<bool> StartAsync()
        {
            var task = Task.Run(() =>
            {
                this.isRunning = true;
                this.stopwatch.Restart();
                ArrayList arrayList = new ArrayList(ht.Keys);
                arrayList.Sort();
                while (this.isRunning)
                {
                    if (this.ht.Count > 0 && ((TimeSpan)arrayList[0]) <= this.stopwatch.Elapsed)
                    {
                        this.DataReceived?.Invoke(this, new DataReceivedEventArgs((byte[])ht[arrayList[0]]) { Time = stopwatch.Elapsed });
                        this.ht.Remove(arrayList[0]);
                        arrayList.RemoveAt(0);
                    }
                }
                return true;
            });
            return task;
        }
        public Task<bool> StopAsync()
        {
            var task = Task.Run(() =>
            {
                this.stopwatch.Stop();
                this.isRunning = false; return true;
            });
            return task;
        }
        #endregion
    }
}
